package com.songmy.spider.run.javaSpider;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.songmy.spider.run.vo.SpiderRule;

/**
 * Hello world!
 *
 */
public class App {
	
	private static final String _CHARSET = "UTF-8";
	
	public static void main(String[] args) throws FileNotFoundException {
		System.out.println("Hello World!");
		String fullFileName = "D:\\workspace\\javamy\\javaSpider\\src\\main\\java\\zhihu.json";
		JSONObject jObject =  JSONObject.parseObject(jsonValue(fullFileName));
		SpiderRule rule = JSON.parseObject(jObject.toJSONString(), SpiderRule.class);
		
		try {
			MySpider.start(rule);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		System.out.println(jObject.toJSONString());
		
//		String a ="https://www\\.zhihu\\.com/people/\\[a-zA-z|-\\]+/followees";
//		System.err.println(a);
	}

	public static String jsonValue(String filename) {

		File file = new File(filename);
		Scanner scanner = null;
		StringBuilder buffer = new StringBuilder();
		try {
			scanner = new Scanner(file, _CHARSET);
			while (scanner.hasNextLine()) {
				buffer.append(scanner.nextLine());
			}

		} catch (FileNotFoundException e) {

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
		return buffer.toString();
	}
}
